<template>
	<view>
		<view style="font-size:28rpx;line-height:20px;color: #999;padding:20px;">
			提示：</br>必得分红猫进度默认为4等分模式：看视频次数，邀请人数，合成猫次数、旅行团收益4部分组成，
			每部分各占25%，某一部分达到25%以后不再上涨。</br>必得进度达到100%以后，将获得一只永久分红猫，必得进度将不再变化。</br>旅行团收益：为好友给我赚钱的进度，例如设置旅行团收益10元，必得进度1%，单次收益没有达到10元，进度不会增加，直到累计收益达到10元，必得进度才会增加1%。</br>若开启了必得分红猫设置为单一模式：视频次数，邀请人数，合成猫次数、旅行团收益只要其中一项达到100%即可获得永久分红猫。</br>配置参数保存后，前端预计10分钟左右生效。
		</view>
		<view class="uni-container" style="float: left;">
			<uni-forms ref="form" validateTrigger="bind">
				<uni-forms-item label="看视频次数" name="title" labelWidth="100">
					<input class="uni-input-border" type="number" v-model="bide.videocount" />
				</uni-forms-item>
				<uni-forms-item label="邀请人数" name="title" labelWidth="100">
					<input class="uni-input-border" type="number" v-model="bide.friendcount" />
				</uni-forms-item>
				<uni-forms-item label="合成次数" name="title" labelWidth="100">
					<input class="uni-input-border" type="number" step="0.1" v-model="bide.hechengcount" />
				</uni-forms-item>
				<uni-forms-item label="旅行团收益" name="title" labelWidth="100">
					<input class="uni-input-border" type="input" v-model="bide.friendfen" />
				</uni-forms-item>
				<view class="uni-button-group pointer">
					<button class="uni-button uni-button-full" type="primary" :disabled="isLoading"
						@click="save">保存</button>
				</view>
			</uni-forms>
		</view>

		<view class="uni-container" style="float: left;">
			<uni-forms ref="form" validateTrigger="bind">
				<uni-forms-item label="得进度:(万分比)" name="title" labelWidth="130">
					<input class="uni-input-border" type="number" v-model="bideprofit.videocount" />
				</uni-forms-item>
				<uni-forms-item label="得进度:(万分比)" name="title" labelWidth="130">
					<input class="uni-input-border" type="number" v-model="bideprofit.friendcount" />
				</uni-forms-item>
				<uni-forms-item label="得进度:(万分比)" name="title" labelWidth="130">
					<input class="uni-input-border" type="number" v-model="bideprofit.hechengcount" />
				</uni-forms-item>
				<uni-forms-item label="得进度:(万分比)" name="title" labelWidth="130">
					<input class="uni-input-border" type="number" v-model="bideprofit.friendfen" />
				</uni-forms-item>
			</uni-forms>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isLoading: false,
				"bide": {
					"videocount": 0, // 看视频次数
					"friendcount": 0, // 邀请人数
					"hechengcount": 0, // 合成狗次数
					"friendfen": 0, // 旅行团收益   分
				},
				// 满足一次   给多少必得收益
				"bideprofit": {
					"videocount": 0, // 百分比
					"friendcount": 0, // 百分比
					"hechengcount": 0, // 百分比
					"friendfen": 0, // 百分比
				}
			}
		},
		mounted() {
			this.getinfo()
		},
		methods: {
			getinfo() {
				this.isLoading = true
				uni.showLoading({
					title: '加载中'
				});
				uniCloud.callFunction({
					name: 'uni-admin',
					data: {
						action: "admin/loadconfig",
						data: {
							id: 'bide'
						}
					}
				}).then((res) => {
					console.log(res, "get--------------")
					this.isLoading = false
					this.bide = res.result.config
					uni.hideLoading();
				}).catch((res) => {
					console.log(res)
				}).finally(err => {
					this.isLoading = false
					uni.hideLoading();
				})
				
				uniCloud.callFunction({
					name: 'uni-admin',
					data: {
						action: "admin/loadconfig",
						data: {
							id: 'bideprofit'
						}
					}
				}).then((res) => {
					console.log(res, "get--------------")
					this.isLoading = false
					var json = res.result.config
					var obj = {}
					for (let key in json) {
						if (key != '_id') {
							obj[key] = json[key] * 100
						}
					}
					this.bideprofit = obj
					uni.hideLoading();
				}).catch((res) => {
					console.log(res)
				}).finally(err => {
					this.isLoading = false
					uni.hideLoading();
				})
			},
			save() {
				this.isLoading = true
				delete this.bide._id;
				var obj = {}
				for (let key in this.bideprofit) {
					if (key != '_id') {
						obj[key] = parseFloat(this.bideprofit[key]) / 100
					}
				}
				for (let key in this.bide) {
					if (key != '_id') {
						this.bide[key] = parseFloat(this.bide[key])
					}
				}
				console.log("this.bide: ",this.bide);
				console.log("obj: ",obj);
				
				uni.showLoading({
					title: '保存中'
				});
				uniCloud.callFunction({
					name: 'uni-admin',
					data: {
						action: "admin/setconfig",
						data: {
							json: JSON.stringify({
								bideprofit: obj
							})
						}
					}
				}).then((res) => {
					console.log(res, "res--------------")
					this.isLoading = false
					uni.hideLoading();
					uni.showToast({
						title: "修改成功",
						icon: 'none',
						duration: 2000
					})
				}).catch((res) => {
					console.log(res)
				}).finally(err => {
					this.isLoading = false
					uni.hideLoading();
				})
				
				uniCloud.callFunction({
					name: 'uni-admin',
					data: {
						action: "admin/setconfig",
						data: {
							json: JSON.stringify({
								bide: this.bide
							})
						}
					}
				}).then((res) => {
					console.log(res, "res--------------")
					this.isLoading = false
					uni.hideLoading();
					uni.showToast({
						title: "修改成功",
						icon: 'none',
						duration: 2000
					})
				}).catch((res) => {
					console.log(res)
				}).finally(err => {
					this.isLoading = false
					uni.hideLoading();
				})
			}
		}
	}
</script>

<style>

</style>
